home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / cvt100.zip / COMIO.C next >
Text File  |  1988-07-31  |  14KB  |  337 lines

  1. #include <stdio.h>
  2. #include <dos.h>
  3.  
  4. #define MDMDAT1 0x03F8            /* Address of modem port 1 data */
  5. #define MDMSTS1 0x03FD            /* Address of modem port 1 status  */
  6. #define MDMCOM1 0x03FB            /* Address of modem port 1 command */
  7. #define MDMDAT2 0x02F8            /* Address of modem port 2 data */
  8. #define MDMSTS2 0x02FD            /* Address of modem port 2 status */
  9. #define MDMCOM2 0x02FB            /* Address of modem port 2 command */
  10. #define MDMINTV 0x000C            /* Com 1 interrupt vector */
  11. #define MDINTV2 0x000B            /* Com 2 interrupt vector */
  12. #define MDMINTO 0x0EF             /* Mask to enable IRQ3 for port 1 */
  13. #define MDINTO2 0x0F7             /* Mask to enable IRQ4 for port 2 */
  14. #define MDMINTC 0x010             /* Mask to Disable IRQ4 for port 1 */
  15. #define MDINTC2 0x008             /* Mask to Disable IRQ3 for port 2 */
  16. #define INTCONT 0x0021            /* 8259 interrupt controller ICW2-3 */
  17. #define INTCON1 0x0020            /* Address of 8259 ICW1 */
  18.  
  19. #define COM_BUFF_SIZE 1024        /* Communications port buffer size */
  20. #define XOFFPT  COM_BUFF_SIZE*3/4 /* chars in buff before sending XOFF */
  21. #define XONPT   COM_BUFF_SIZE*1/4 /* chars in buff to send XON after XOFF */
  22. #define XOFF    0x13              /* XOFF value */
  23. #define XON     0x11              /* XON value */
  24.  
  25. /*****************************************************************************/
  26. /* function prototypes                                                       */
  27.  
  28. void TTinit();                    /* Initialize the communications system */
  29. int ttopen();                     /* Open a port for communications */
  30. int ttclose( void );              /* Close the communications port */
  31. int ttchk( void );                /* Return count of received characters */
  32. void ttoc( unsigned char );       /* Output a character to the com port */
  33. int ttinc( void );                /* Input a character from circular buffer */
  34. void ttflui( void );              /* Flush circular buffer of characters */
  35. int dobaud( unsigned int );       /* Set the baud rate for the port */
  36. void coms( int );                 /* Establish modem data */
  37. void serini( void );              /* Initialize the com port for interrupts */
  38. void serrst( void );              /* Reset the com port to original settings */
  39. void interrupt serint( void );    /* Com port receiver ISR */
  40.  
  41.  
  42. /*****************************************************************************/
  43. /* Global Data                                                               */
  44.  
  45. unsigned int port;                /* COM port */
  46. unsigned int speed;               /* BAUD rate */
  47. char parity[5];                   /* Parity setting */
  48. unsigned int databits;            /* Number of Data bits */
  49. unsigned int stopbits;            /* Number of Stop bits */
  50.  
  51. /*****************************************************************************/
  52. /* External variables                                                        */
  53.  
  54.  
  55.  
  56. /*****************************************************************************/
  57. /* Local Static Data                                                         */
  58.  
  59. static char buffer[COM_BUFF_SIZE];/* Circular buffer */
  60. static char *inptr;               /* Pointer to input point of circular buff*/
  61. static char *outptr;              /* Pointer to output point of circular buff*/
  62. static int  count = 0;            /* Number of characters in buffer */
  63.  
  64. struct mdminfo {                  /* struct to hold current com port info */
  65.     unsigned int mddat;             /* 8250 data register */
  66.     unsigned int mdstat;            /* 8250 line-status register */
  67.     unsigned int mdcom;             /* 8250 line-control register */
  68.     unsigned char mden;             /* 8259 IRQ enable mask */
  69.     unsigned char mddis;            /* 8259 IRQ disable mask */
  70.     unsigned char mdintv;           /* Interrupt for selected com port */
  71. } modem ;
  72.  
  73. void interrupt (*oldvec)();       /* Vector of previous com interrupt */
  74. int portin = 0;                   /* Flag to indicate com port is open */
  75. int xofsnt = 0;                   /* Flag to indicate an XOFF transmitted */
  76. int xofrcv = 0;                   /* Flag to indicate an XOFF received */
  77.  
  78. /*****************************************************************************/
  79.  
  80. /*  T T I N I T  -- Initialize the communications system */
  81.  
  82. void TTinit() {
  83.  
  84.     if (GetTTSetup() == 0) {        /* If no saved values are available */
  85.         port = 1;                     /* Then set default values */
  86.         speed = 2400;
  87.         strcpy(parity,"NONE");
  88.         databits = 8;
  89.         stopbits = 1;
  90.     }
  91. }
  92.  
  93.  
  94. /*  T T O P E N  -- Open the communications port */
  95.  
  96. ttopen() {
  97.     if (portin == 0) {            /* Ignore call if already open */
  98.         switch (port) {
  99.             case 1:
  100.                 coms(1);             /* COM 1 */
  101.                 break;
  102.             case 2:
  103.                 coms(2);             /* COM 2 */
  104.                 break;
  105.             default:                 /* others not supported, return error */
  106.                 return(-1);
  107.         }
  108.         dobaud(speed);               /* Set baud rate */
  109.         serini();                    /* enable interrupt handler */
  110.     }
  111.     return(0);                    /* return success */
  112. }
  113.  
  114.  
  115. /*  T T C L O S E --  Close the communications port  */
  116.  
  117. ttclose() {
  118.     if (portin != 0)              /* Ignore if port is already closed */
  119.         serrst();                    /* otherwise disable interrupts */
  120.     return(0);                    /* return success */
  121. }
  122.  
  123.  
  124.  
  125.  
  126. /* T T C H K  --  Return a count of characters at the serial port */
  127.  
  128. ttchk() {
  129.     return( count );              /* return maintained count */
  130. }
  131.  
  132.  
  133. /* T T O C -- Output a character to the current serial port */
  134.  
  135. void ttoc( unsigned char c ) {
  136.  
  137.     while( (inportb(modem.mdstat) & 0x20) == 0 )
  138.        ;                          /* Wait til transmitter is ready */
  139.     outportb(modem.mddat,c);      /* then output the character */
  140. }
  141.  
  142.  
  143. /* T T F L U I  --  Clear the input buffer of characters */
  144.  
  145.  
  146. void ttflui() {
  147.  
  148.     if (xofsnt){                  /* Check if XON should be sent after XOFF */
  149.        xofsnt = 0;                  /* if so then reset XOFF sent status */
  150.        ttoc(XON);                   /* and send the XON */
  151.        }
  152.     disable();                    /* NO interrupts allowed now */
  153.     inptr = outptr = buffer;      /* Reset input out output pointers */
  154.     count = 0;                    /* Set received characters count to 0 */
  155.     enable();                     /* Now interrupts are ok */
  156. }
  157.  
  158.  
  159. /* T T I N C  -- Read a character from serial ports circular buffer */
  160.  
  161. ttinc() {
  162.     int c;
  163.     register char * ptr;
  164.  
  165.     if (count < XONPT && xofsnt){ /* Check if XON should be sent after XOFF */
  166.        xofsnt = 0;                  /* if so then reset XOFF sent status */
  167.        ttoc(XON);                   /* and send the XON */
  168.        }
  169.  
  170.     while (count == 0)            /* If no characters have arrived then */
  171.         ;                            /* wait til one arrives */
  172.  
  173.     ptr = outptr;                 /* Save address of buffer output point */
  174.  
  175.     c = *ptr++;                   /* Get this character and increment ptr */
  176.  
  177.                                   /* See if circular buff should be wrapped */
  178.     if (ptr == &buffer[COM_BUFF_SIZE])
  179.         ptr = buffer;                /* if so then save new output point */
  180.  
  181.     disable();                    /* NO interrupts allowed now */
  182.     outptr = ptr;                 /* Save the address of output point */
  183.     count--;                      /* Decrement count of received characters */
  184.     enable();                     /* Interrupts can continue now */
  185.  
  186.     return(c);                    /* Return the received character */
  187. }
  188.  
  189.  
  190. /* D O B A U D  --  Set the baud rate for the current port */
  191.  
  192. dobaud( unsigned int baudrate ) {
  193.    unsigned char portval;
  194.    unsigned char blo, bhi;
  195.    switch (baudrate) {            /* Get 8250 baud rate divisor values */
  196.        case 50:     bhi = 0x